package cn.waka.sys.admin.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.waka.sys.admin.dao.StarDao;
import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.Star;
import cn.waka.sys.admin.object.Starimg;

@Service
public class StarService {

	@Autowired
	private StarDao starDao;

	public Page findPage(Page p,Map<String,String> param){
		List<Star> list=starDao.findList(p,param);
		for(int i=0;i<list.size();i++){
			List<Starimg> imgs = starDao.getStarImgs(list.get(i).getId());
			list.get(i).setImgs(imgs);
		}
		int count=starDao.findCount(p,param);
		p.setData(list);
		p.setCount(count);
		int totalCount=1;
		if(count%p.getSize()>0){
			totalCount=(int) Math.floor(count/p.getSize());
			totalCount++;
		}else{
			totalCount=count/p.getSize();
		}
		p.setTotalPage(totalCount);
		return p;
	}
	
	public Star findById(int id){
		return starDao.findById(id);
	}
	
	public int saveStar(Star r){
		int rs = 1;
		if(r.getId()>0){
			rs = starDao.updateStar(r);
		}else{
			rs = starDao.insert(r);
		}
		return rs;
	}
	
	public int del(int id) {
		int rs = starDao.del(id);
		if(rs>0){
			starDao.delImg(id);
		}
		return rs;
	}
	
	public int delStarImg(int id) {
		return starDao.delStarImg(id);
	}
	
	public int saveImg(int starId,String img) {
		return starDao.saveImg(starId,img);
	}
}
